<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:s="http://www.w3.org/2003/05/soap-envelope"
    xmlns:drive="http://schemas.microsoft.com/robotics/2006/05/drive.html"
    xmlns:motor="http://schemas.microsoft.com/robotics/2006/05/motor.html"
    xmlns:physical="http://schemas.microsoft.com/robotics/2006/07/physicalmodel.html">

  <xsl:import href="/resources/dss/Microsoft.Dss.Runtime.Home.MasterPage.xslt" />

  <xsl:template match="/">
    <xsl:call-template name="MasterPage">
      <xsl:with-param name="serviceName">
        <img src="/resources/Hemisson.Y2008.M01/ProMRDS.Robotics.Hemisson.Resources.HemissonService.Image.png" align="middle"/> Hemisson Generic Drive
      </xsl:with-param>
      <xsl:with-param name="description">
        Provides access to the Hemisson Drive (Uses the Generic Drive contract)
      </xsl:with-param>
      <xsl:with-param name="head">
        <style type="text/css">
          /* Service-specific stylesheet goes here */
          input.TextBox { width: 70px; border: 1px solid #666666; font-weight: bold; }
        </style>
        <script language="javascript" type="text/javascript">
          <![CDATA[<!--

/* Service-specific script goes here */

dssRuntime.init = function()
{
  // Add page initialization code here.
  // This function is attached to the window.onload event.
  // Do not override window.onload.
}

// Check that a value is numeric and within a specified range
// Returns a string which is either empty or an error message
function validate(val, fieldname, minval, maxval)
{
  var msg = "";
  var num;
  if (val.length == 0)
  {
     msg += "Please enter a value for " + fieldname + "\n";
  }
  else
  {
    num = parseFloat(val);
    if (isNaN(num))
      msg += fieldname + " must be a number\n";
    else if (num < minval || num > maxval)
      msg += fieldname + " is out of range (" + minval + " to " + maxval +")\n";
  }
  return msg;
}

// Check the form before submission
// NOTE: Return true if OK to proceed, or false if there are errors
function checkform(f)
{
  var msg = "";

  // Accumulate all of the error messages (if there are any)
  msg += validate(f.LeftPower.value, "Left Power", -1.0, 1.0);
  msg += validate(f.RightPower.value, "Right Power", -1.0, 1.0);

  // If there were any errors at all, display them
  if (msg != "")
  {
    // Pop up a dialog to tell the user what is wrong
    alert(msg);
    return false;
  }

  // If we got here, then the data is OK
  return true;
}

//-->     ]]>
        </script>
      </xsl:with-param>
    </xsl:call-template>
  </xsl:template>

  <xsl:template match="s:Header">

  </xsl:template>

  <xsl:template match="drive:DriveDifferentialTwoWheelState">
    <form name="DssForm" method="post" onsubmit="return checkform(this);">
      <table width="100%" border="0" cellpadding="5" cellspacing="5">
        <tr>
          <th colspan="3">
            Hemisson Drive
          </th>
        </tr>
        <tr>
          <td width="33%">
            <input type="submit" name="Stop" value="Stop" />
          </td>
          <td width="33%">
            <input type="submit" name="SetPower" value="Set Power" />
          </td>
          <td width="33%">
            <xsl:if test="drive:IsEnabled = 'true'">
              <input type="submit" name="DisableDrive" value="Disable Drive" />
            </xsl:if>
            <xsl:if test="drive:IsEnabled = 'false'">
              <input type="submit" name="EnableDrive" value="Enable Drive" />
            </xsl:if>
          </td>
        </tr>
      </table>
      <table width="100%" border="0" cellpadding="5" cellspacing="5">
        <tr>
          <th width="20%">Enabled</th>
          <td width="80%">
            <xsl:value-of select="drive:IsEnabled"/>
          </td>
        </tr>
        <tr>
          <th width="20%">Drive State</th>
          <td width="80%">
            <xsl:value-of select="drive:DriveState"/>
          </td>
        </tr>
        <tr>
          <td colspan="2">
            <table width="100%" border="0" cellpadding="5" cellspacing="5">
              <tr>
                <th width="20%">Property</th>
                <th width="40%">Left Wheel</th>
                <th width="40%">Right Wheel</th>
              </tr>
              <tr>
                <th width="20%">
                  Name:
                </th>
                <td width="40%">
                  <xsl:value-of select="drive:LeftWheel/motor:MotorState/motor:Name"/>
                </td>
                <td width="40%">
                  <xsl:value-of select="drive:RightWheel/motor:MotorState/motor:Name"/>
                </td>
              </tr>
              <tr class="odd">
                <th width="20%">
                  Identifier:
                </th>
                <td width="40%">
                  <xsl:value-of select="drive:LeftWheel/motor:MotorState/motor:HardwareIdentifier"/>
                </td>
                <td width="40%">
                  <xsl:value-of select="drive:RightWheel/motor:MotorState/motor:HardwareIdentifier"/>
                </td>
              </tr>
              <tr>
                <th width="20%">
                  Current Power:
                </th>
                <td width="40%">
                  <input type="text" name="LeftPower" class="TextBox">
                    <xsl:attribute name="value">
                      <xsl:value-of select="drive:LeftWheel/motor:MotorState/motor:CurrentPower"/>
                    </xsl:attribute>
                  </input>
                </td>
                <td width="40%">
                  <input type="text" name="RightPower" class="TextBox">
                    <xsl:attribute name="value">
                      <xsl:value-of select="drive:RightWheel/motor:MotorState/motor:CurrentPower"/>
                    </xsl:attribute>
                  </input>
                </td>
              </tr>
              <tr class="odd">
                <th width="20%">
                  Power Scaling Factor:
                </th>
                <td width="40%">
                  <xsl:value-of select="drive:LeftWheel/motor:MotorState/motor:PowerScalingFactor"/>
                </td>
                <td width="40%">
                  <xsl:value-of select="drive:RightWheel/motor:MotorState/motor:PowerScalingFactor"/>
                </td>
              </tr>
              <tr>
                <th width="20%">
                  Reverse Polarity:
                </th>
                <td width="40%">
                  <xsl:value-of select="drive:LeftWheel/motor:MotorState/motor:ReversePolarity"/>
                </td>
                <td width="40%">
                  <xsl:value-of select="drive:RightWheel/motor:MotorState/motor:ReversePolarity"/>
                </td>
              </tr>
              <tr>
                <th width="20%">Pose Position:</th>
                <td width="40%">
                  (<xsl:value-of select="drive:LeftWheel/motor:MotorState/motor:Pose/physical:Position/physical:X"/>,
                  <xsl:value-of select="drive:LeftWheel/motor:MotorState/motor:Pose/physical:Position/physical:Y"/>,
                  <xsl:value-of select="drive:LeftWheel/motor:MotorState/motor:Pose/physical:Position/physical:Z"/>)
                </td>
                <td width="40%">
                  (<xsl:value-of select="drive:RightWheel/motor:MotorState/motor:Pose/physical:Position/physical:X"/>,
                  <xsl:value-of select="drive:RightWheel/motor:MotorState/motor:Pose/physical:Position/physical:Y"/>,
                  <xsl:value-of select="drive:RightWheel/motor:MotorState/motor:Pose/physical:Position/physical:Z"/>)
                </td>
              </tr>
              <tr>
                <th width="20%">Pose Orientation:</th>
                <td width="40%">
                  (<xsl:value-of select="drive:LeftWheel/motor:MotorState/motor:Pose/physical:Orientation/physical:X"/>,
                  <xsl:value-of select="drive:LeftWheel/motor:MotorState/motor:Pose/physical:Orientation/physical:Y"/>,
                  <xsl:value-of select="drive:LeftWheel/motor:MotorState/motor:Pose/physical:Orientation/physical:Z"/>,
                  <xsl:value-of select="drive:LeftWheel/motor:MotorState/motor:Pose/physical:Orientation/physical:W"/>)
                </td>
                <td width="40%">
                  (<xsl:value-of select="drive:RightWheel/motor:MotorState/motor:Pose/physical:Orientation/physical:X"/>,
                  <xsl:value-of select="drive:RightWheel/motor:MotorState/motor:Pose/physical:Orientation/physical:Y"/>,
                  <xsl:value-of select="drive:RightWheel/motor:MotorState/motor:Pose/physical:Orientation/physical:Z"/>,
                  <xsl:value-of select="drive:RightWheel/motor:MotorState/motor:Pose/physical:Orientation/physical:W"/>)
                </td>
              </tr>
            </table>
          </td>
        </tr>
      </table>
    </form>
  </xsl:template>
</xsl:stylesheet>
